package com.openlogic.logic.concurent;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.noear.solon.cloud.CloudClient;

import com.openlogic.api.logic.dto.RuleSqlModelColumnDto;
import com.openlogic.api.logic.entity.RuleSqlModelColumn;

import jodd.db.QueryMapper;
import jodd.db.connection.ConnectionProvider;
import jodd.db.oom.DbOomQuery;

/**
* @ClassName: RuleSqlColumnHander
* @Description: sql参数处理器
* @author weiyongpeng
* @date 2024年11月24日 下午6:15:34
 */
public class RuleSqlColumnHander extends CallableTemplate<RuleSqlModelColumnDto> {

    private CountDownLatch latch;

    private ConnectionProvider connectionProvider;

    private String sql;
    
    private String ruleFactId;
    
    private List<Map<String, String>> columnMeta;
    
    private static RuleSqlModelColumnDto ruleSqlModelColumn = new RuleSqlModelColumnDto();

    public RuleSqlColumnHander(CountDownLatch latch, ConnectionProvider connectionProvider, String pageSql, String ruleFactId, List<Map<String, String>> columnMeta) {
		this.latch = latch;
		this.connectionProvider = connectionProvider;
		this.sql = pageSql;
		this.ruleFactId = ruleFactId;
		this.columnMeta = columnMeta;
	}

	@Override
    public RuleSqlModelColumnDto process() {
        log.info("执行sql:" + sql);
        long start = System.currentTimeMillis();
        // 执行sql
        List<RuleSqlModelColumn> list = DbOomQuery.query(connectionProvider.getConnection(), sql).list(new QueryMapper<RuleSqlModelColumn>() {

			@Override
			public RuleSqlModelColumn process(ResultSet rs) throws SQLException {
				// 获取结果集的元数据信息
                ResultSetMetaData rsmd = rs.getMetaData();
                // 获取列字段的个数
                int colunmCount = rsmd.getColumnCount();
                RuleSqlModelColumn modelColumn = new RuleSqlModelColumn();
				for (int i = 1; i <= colunmCount; i++) {
					// 获取列名
					String columnName = rsmd.getColumnName(i); // 数据项key
					String columnLabel = columnMeta.get(i-1).get(columnName); // 数据项的备注
					String dbDataType = "1"; // 默认是String
					switch (rsmd.getColumnType(i)) {
					case Types.ARRAY:
						break;
					case Types.SMALLINT:
					case Types.INTEGER:
					case Types.BIGINT:
						dbDataType = "2";
						break;
					case Types.BOOLEAN:
					case Types.BIT:
						dbDataType = "5";
						break;
					case Types.NUMERIC:
					case Types.DECIMAL:
					case Types.DOUBLE:
					case Types.FLOAT:
					case Types.REAL:
						dbDataType = "4";
						break;
					case Types.NVARCHAR:
					case Types.NCHAR:
					case Types.LONGNVARCHAR:
					case Types.VARCHAR:
					case Types.CHAR:
					case Types.LONGVARCHAR:
						dbDataType = "1";
						break;
					case Types.TINYINT:
					case Types.BINARY:
					case Types.VARBINARY:
						break;
					case Types.DATE:
					case Types.TIME:
					case Types.TIMESTAMP:
						dbDataType = "3";
						break;
					case Types.BLOB:
					case Types.CLOB:
					case Types.LONGVARBINARY:
					case Types.DATALINK:
					case Types.REF:
					case Types.STRUCT:
					case Types.DISTINCT:
					case Types.JAVA_OBJECT:
						break;
					default:
						dbDataType = "1";
						break;
					}
					long id = CloudClient.id().generate();
					modelColumn.setColumnKey(columnName);
					modelColumn.setColumnName(columnLabel);
					modelColumn.setColumnType(dbDataType);
					modelColumn.setId(String.valueOf(id));
					modelColumn.setRuleFactId(ruleFactId);
				}
				return modelColumn;
			}
        });
        latch.countDown();
        long end = System.currentTimeMillis();
        log.info("线程查询数据用时:" + (end - start) + "ms");
        ruleSqlModelColumn.setRuleSqlModelColumns(list);
        return ruleSqlModelColumn;
    }
}
